﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.IO;
using System.Xml;
using MyPhotoIndex.Interfaces.Imaging;

namespace MyPhotoIndex.Plugins.WebHost.RSS
{
    public class MediaRSSSerializer
    {
        public static string GetMediaRss(IImageData[] imageList, string baseUrl, string channelIcon)
        {
            if (imageList == null ||
                imageList.Length == 0)
            {
                return string.Empty;
            } 

            using (Stream ms = new MemoryStream())
            {
                using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8))
                {
                    writer.WriteStartDocument(true);
                    writer.WriteStartElement("rss");
                    writer.WriteAttributeString("version", "2.0");
                    writer.WriteAttributeString("xmlns:media", "http://search.yahoo.com/mrss/");
                    writer.WriteAttributeString("xmlns:atom", "http://www.w3.org/2005/Atom");
                    writer.WriteStartElement("channel");

                    writer.WriteStartElement("atom:link");
                    writer.WriteAttributeString("href", baseUrl + "/RSSFeed.mvp");
                    writer.WriteAttributeString("rel", "self");
                    writer.WriteAttributeString("type", "application/rss+xml");
                    writer.WriteEndElement();

                    if (string.IsNullOrEmpty(channelIcon) == false)
                    {
                        writer.WriteStartElement("atom:icon");
                        writer.WriteString(channelIcon);
                        writer.WriteEndElement();
                    }

                    foreach (IImageData imageData in imageList)
                    {
                        writer.WriteStartElement("item");

                        writer.WriteStartElement("title");
                        writer.WriteString(imageData.Id.ToString());
                        writer.WriteEndElement();

                        writer.WriteStartElement("media:description");
                        writer.WriteString(imageData.Comment);
                        writer.WriteEndElement();

                        writer.WriteStartElement("link");
                        writer.WriteString(baseUrl + "/images/" + imageData.Id.ToString() + ".jpg");
                        writer.WriteEndElement();
                        
                        writer.WriteStartElement("media:thumbnail");
                        writer.WriteAttributeString("url", baseUrl + "/thumbnails/" + imageData.Id.ToString() +".jpg");
                        writer.WriteEndElement();

                        writer.WriteStartElement("media:content");
                        writer.WriteAttributeString("url", baseUrl + "/images/" + imageData.Id.ToString() + ".jpg" + "?w=800&h=600");
                        writer.WriteEndElement();

                        writer.WriteEndElement();
                    }

                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.Flush();

                    ms.Position = 0;
                    using (StreamReader sr = new StreamReader(ms))
                    {
                        return sr.ReadToEnd();
                    }
                }
            }
        }
    }
}
